package com.simpleframe.validator.mapper;

import java.util.ArrayList;
import java.util.List;

public class ValidateField {
	
	private String name;
	private String label;
	
	private List<ValidateRule> rules;
	
	public ValidateField(String name, String label){
		if(name==null){
			throw new IllegalArgumentException("name 不能为空");
		}
		this.name = name;
		this.label = label;
	}
	
	public void addRule(ValidateRule validateRule){
		if(rules==null)
			rules = new ArrayList<ValidateRule>();
		rules.add(validateRule);
	}
	
	/**
	 * 把配置文件里的规则转换成JS的规则
	 * @return
	 */
	public String getInputClass(){
		StringBuffer htmlBuffer = new StringBuffer();
		for (ValidateRule rule : rules) {
			htmlBuffer.append(rule.getType());
			htmlBuffer.append(":");
			htmlBuffer.append(rule.getValue());
			htmlBuffer.append(",");
		}
		htmlBuffer.delete(htmlBuffer.lastIndexOf(","), htmlBuffer.length());
		return htmlBuffer.toString();
	}
	
	//setters and getters

	public String getName() {
		return name;
	}

	public String getLabel() {
		return label;
	}

	public List<ValidateRule> getRules() {
		return rules;
	}

	public void setRules(List<ValidateRule> rules) {
		this.rules = rules;
	}
	
	
}
